package com.buli.util.file;

import com.google.common.collect.ImmutableMap;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * FileType 枚举类，用于定义文件类型与其对应的 MIME 类型
 */
public enum FileType {
    // 定义各种文件类型的扩展名和 MIME 类型
    APK("apk", "application/vnd.android.package-archive"),
    AVI("avi", "video/x-msvideo"),
    BMP("bmp", "image/bmp"),
    CSS("css", "text/css"),
    CSV("csv", "text/csv"),
    DOC("doc", "application/msword"),
    DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"),
    EOT("eot", "application/vnd.ms-fontobject"),
    EXE("exe", "application/octet-stream"), // 添加 exe 格式
    FLAC("flac", "audio/flac"),
    GIF("gif", "image/gif"),
    GZ("gz", "application/gzip"),
    HTML("html", "text/html"),
    JPEG("jpeg", "image/jpeg"),
    JPG("jpg", "image/jpeg"),
    JS("js", "application/javascript"),
    JSON("json", "application/json"),
    MKV("mkv", "video/x-matroska"),
    MOV("mov", "video/quicktime"),
    MP3("mp3", "audio/mpeg"),
    MP4("mp4", "video/mp4"),
    OGG("ogg", "audio/ogg"),
    OTF("otf", "font/otf"),
    PDF("pdf", "application/pdf"),
    PNG("png", "image/png"),
    PPT("ppt", "application/vnd.ms-powerpoint"),
    PPTX("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"),
    RAR("rar", "application/x-rar-compressed"),
    SVG("svg", "image/svg+xml"),
    TAR("tar", "application/x-tar"),
    TTF("ttf", "font/ttf"), // 新增文件类型
    TXT("txt", "text/plain"),
    WAV("wav", "audio/wav"),
    WEBP("webp", "image/webp"),
    WOFF("woff", "font/woff"),
    WOFF2("woff2", "font/woff2"),
    XLS("xls", "application/vnd.ms-excel"),
    XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
    XML("xml", "application/xml"),
    ZIP("zip", "application/zip"); // 添加 apk 格式

    // 定义文件扩展名和 MIME 类型变量
    private final String extension;
    private final String mimeType;

    // 预先构建扩展名到 MIME 类型和 MIME 类型到扩展名的映射，以提高查询效率
    private static final ImmutableMap<String, String> extensionToMimeTypeMap = ImmutableMap.copyOf(buildExtensionToMimeTypeMap());
    private static final ImmutableMap<String, String> mimeTypeToExtensionMap = ImmutableMap.copyOf(buildMimeTypeToExtensionMap());
    // 日志记录器，用于记录错误或警告信息
    private static final Logger logger = Logger.getLogger(FileType.class.getName());

    // 静态代码块，用于初始化日志记录器等静态资源
    static {
        // 初始化 Map
    }

    // 构建从扩展名到 MIME 类型的映射
    private static Map<String, String> buildExtensionToMimeTypeMap() {
        Map<String, String> map = new HashMap<>();
        for (FileType fileType : values()) {
            map.put(fileType.extension.toLowerCase(), fileType.mimeType);
        }
        return map;
    }

    // 构建从 MIME 类型到扩展名的映射
    private static Map<String, String> buildMimeTypeToExtensionMap() {
        Map<String, String> map = new HashMap<>();
        for (FileType fileType : values()) {
            map.put(fileType.mimeType, fileType.extension);
        }
        return map;
    }

    // 构造函数，初始化文件类型
    FileType(String extension, String mimeType) {
        this.extension = extension;
        this.mimeType = mimeType;
    }

    // 获取 MIME 类型
    public String getMimeType() {
        return mimeType;
    }

    // 根据文件扩展名获取 MIME 类型
    public static String getMimeTypeByExtension(String extension) {
        try {
            if (extension == null || extension.trim().isEmpty()) {
                logger.warning("Invalid extension: " + extension);
                return "application/octet-stream";
            }
            String lowerCaseExtension = extension.trim().toLowerCase();
            return extensionToMimeTypeMap.getOrDefault(lowerCaseExtension, "application/octet-stream");
        } catch (NullPointerException | IllegalArgumentException e) {
            logger.severe("Error getting MIME type by extension: " + e.getMessage());
            return "application/octet-stream";
        }
    }

    // 根据 MIME 类型获取文件扩展名
    public static String getExtensionByMimeType(String mimeType) {
        try {
            if (mimeType == null || mimeType.trim().isEmpty()) {
                logger.warning("Invalid MIME type: " + mimeType);
                return "bin";
            }
            return mimeTypeToExtensionMap.getOrDefault(mimeType, "bin");
        } catch (NullPointerException | IllegalArgumentException e) {
            logger.severe("Error getting extension by MIME type: " + e.getMessage());
            return "bin";
        }
    }

    // 添加自定义文件类型
    public static void addCustomFileType(String extension, String mimeType) {
        if (extension == null || extension.trim().isEmpty() || mimeType == null || mimeType.trim().isEmpty()) {
            logger.warning("Invalid input for custom file type.");
            return;
        }
        extensionToMimeTypeMap.put(extension.trim().toLowerCase(), mimeType.trim());
        mimeTypeToExtensionMap.put(mimeType.trim(), extension.trim().toLowerCase());
        logger.info("Added custom file type: " + extension + " -> " + mimeType);
    }
}
